const uuid = require("../utils/uuid");
//统一导出方案使用exports.xxxx
//新增单条数据
exports.modelCreate = async (model, body, res) => {
  const result = await model.create({ _id: uuid, ...body });
  if (result) {
    res.json({
      code: 200,
      msg: "添加成功",
      data: result,
      succeed: true,
    });
    return;
  }
  res.json({
    code: 401,
    msg: "添加失败",
    data: result,
    succeed: false,
  });
};
//修改单条数据
exports.modelAmend = async (res, params, amendParams) => {
  Object.assign(params, amendParams);
  const result = await params.save();
  if (result) {
    res.json({
      code: 200,
      msg: "修改成功",
      data: result,
      succeed: true,
    });
    return;
  }
  res.json({
    code: 400,
    msg: null,
    data: result,
    succeed: false,
  });
};

//删除单条数据
exports.modelDelete = async (model,body,res) => {
  
  const { deletedCount } = await model.deleteOne(body)

  if (!!deletedCount) {
    res.json({
      code: 200,
      data: null,
      msg: "移除成功",
      success: true,
    });
    return
  }
  res.json({
    code: 400,
    data: null,
    msg: "移除失败",
    success: false,
  });
}